﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Devonline.Core;
using Devonline.Entity;

namespace Devonline.Identity
{
    /// <summary>
    /// 访问规则, 字符串类型的默认实现
    /// </summary>
    [Table("access_rule"), DisplayName("访问规则")]
    public class AccessRule : AccessRule<string>
    {
        /// <summary>
        /// 资源
        /// </summary>
        public virtual Resource Resource { get; set; }
    }

    /// <summary>
    /// 访问规则
    /// 简易模式访问规则, 满足规则即允许访问
    /// </summary>
    [Table("access_rule"), DisplayName("访问规则")]
    public class AccessRule<TKey> : EntitySetWithCreateAndUpdate<TKey> where TKey : IConvertible
    {
        /// <summary>
        /// 资源编号
        /// </summary>
        [Column("resource_id"), DisplayName("资源编号"), MaxLength(36), Excel]
        public virtual TKey ResourceId { get; set; }
        /// <summary>
        /// 授权对象
        /// 资源授权使用的主体
        /// </summary>
        [Column("assign_id"), DisplayName("授权对象编号"), MaxLength(36), Excel]
        public virtual TKey AssignId { get; set; }
        /// <summary>
        /// 授权对象类型, AssignType 枚举值
        /// </summary>
        [Column("assign_type", TypeName = "VARCHAR(16)"), DisplayName("授权对象类型"), DefaultValue(AssignType.Role), Excel]
        public virtual AssignType AssignType { get; set; }

        /// <summary>
        /// 规则编号
        /// 针对资源访问的统一规则制定的总体约束, 形如: R35, #35
        /// </summary>
        [Column("code", TypeName = "VARCHAR(16)"), DisplayName("规则编号"), MaxLength(16), Excel]
        public virtual string Code { get; set; }
        /// <summary>
        /// 是否允许 AllowType 枚举值
        /// </summary>
        [Column("allow_type", TypeName = "VARCHAR(16)"), DisplayName("是否允许"), DefaultValue(AllowType.Allow), Excel]
        public virtual AllowType AllowType { get; set; }
        /// <summary>
        /// 优先级
        /// </summary>
        [Column("priority"), DisplayName("优先级"), Excel]
        public virtual int Priority { get; set; }
        /// <summary>
        /// 过期时间限制, 访问授权的有效期
        /// </summary>
        [Column("expire_time"), DisplayName("过期时间"), Excel]
        public virtual DateTime? ExpireTime { get; set; }
        /// <summary>
        /// 访问次数
        /// </summary>
        [Column("access_count"), DisplayName("访问次数"), Excel]
        public virtual int? AccessCount { get; set; }
        /// <summary>
        /// 访问次数限制, 即最大可访问次数
        /// </summary>
        [Column("limit_access_count"), DisplayName("访问次数限制"), Excel]
        public virtual int? LimitAccessCount { get; set; }
        /// <summary>
        /// 条件
        /// 符合条件则执行 AllowType 策略
        /// </summary>
        [Column("condition"), DisplayName("条件"), MaxLength(1024), Excel]
        public virtual string Condition { get; set; }
    }
}
